package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.LuntaiUserCar;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.UUID;

/**
 * @Author lachesism
 * @Date 2025-02-17 21:41
 **/
@RestController
@RequestMapping("/system/upload")
public class uploadController {

    @Value("${file.tyre.address}")
    private String tyreAddress;

    @Value("${url.pc}")
    private String webUrl;

    @PostMapping("/uploadPic")
    public AjaxResult getHundredPic(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            throw new RuntimeException("上传的文件不能为空！");
        }
        try {
            // 确保存储目录存在
            Files.createDirectories(Paths.get(tyreAddress));
            // 生成唯一文件名，防止文件名冲突
            String originalFilename = file.getOriginalFilename();
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
            String newFileName = UUID.randomUUID().toString().replace("-", "") + extension;
            // 目标文件
            File destinationFile = new File(tyreAddress, newFileName);
            file.transferTo(destinationFile);
            // 返回文件访问 URL
            return AjaxResult.success(webUrl + "image/" + newFileName);
        } catch (IOException e) {
            throw new RuntimeException("文件上传失败！", e);
        }
    }
}
